package com.pool.objects;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

public class Ball {
	
	public static final double mass = 0.16;
	public static final double radius = 0.028575;
	
	public BallColor color;
	public double[] position = new double[2];
	
	public Ball(BallColor color) {
	    this.color = color;
	    
	    double triangleSideLength = 10 * Ball.radius;
	    double[] yellowPosition = new double[2];
	    yellowPosition[0] = Table.dimensions[0] * 3/4 - triangleSideLength / Math.sqrt(3);
	    yellowPosition[1] = Table.dimensions[1] / 2;
	    
	    switch (color) {
	    case WHITE:
	    	this.position[0] = Table.dimensions[0] / 4;
	    	this.position[1] = Table.dimensions[1] / 2;
	    	break;
	    	
	    case YELLOW:
	    	this.position[0] = yellowPosition[0];
	    	this.position[1] = yellowPosition[1];
	    	break;
	    	
	    case BLUE:
	    	this.position[0] = yellowPosition[0] + 2 * Math.sqrt(3) * Ball.radius;
	    	this.position[1] = yellowPosition[1] - 2 * Ball.radius;
	    	break;
	    	
	    case RED:
	    	this.position[0] = yellowPosition[0] + 3 * Math.sqrt(3) * Ball.radius;
	    	this.position[1] = yellowPosition[1] + 3 * Ball.radius;
	    	break;
	    	
	    case PURPLE:
	    	this.position[0] = yellowPosition[0] + 4 * Math.sqrt(3) * Ball.radius;
	    	this.position[1] = yellowPosition[1] - 4 * Ball.radius;
	    	break;
	    	
	    case ORANGE:
	    	this.position[0] = yellowPosition[0] + 4 * Math.sqrt(3) * Ball.radius;
	    	this.position[1] = yellowPosition[1];
	    	break;
	    	
	    case GREEN:
	    	this.position[0] = yellowPosition[0] + 4 * Math.sqrt(3) * Ball.radius;
	    	this.position[1] = yellowPosition[1] + 2 * Ball.radius;
	    	break;
	    	
	    case BROWN:
	    	this.position[0] = yellowPosition[0] + 3 * Math.sqrt(3) * Ball.radius;
	    	this.position[1] = yellowPosition[1] - Ball.radius;
	    	break;
	    	
	    case BLACK:
	    	this.position[0] = yellowPosition[0] + 2 * Math.sqrt(3) * Ball.radius;
	    	this.position[1] = yellowPosition[1];
	    	break;
	    	
	    case YELLOW_AND_WHITE:
	    	this.position[0] = yellowPosition[0] + Math.sqrt(3) * Ball.radius;
	    	this.position[1] = yellowPosition[1] + Ball.radius;
	    	break;
	    	
	    case BLUE_AND_WHITE:
	    	this.position[0] = yellowPosition[0] + Math.sqrt(3) * Ball.radius;
	    	this.position[1] = yellowPosition[1] - Ball.radius;
	    	break;
	    	
	    case RED_AND_WHITE:
	    	this.position[0] = yellowPosition[0] + 2 * Math.sqrt(3) * Ball.radius;
	    	this.position[1] = yellowPosition[1] + 2 * Ball.radius;
	    	break;
	    	
	    case PURPLE_AND_WHITE:
	    	this.position[0] = yellowPosition[0] + 4 * Math.sqrt(3) * Ball.radius;
	    	this.position[1] = yellowPosition[1] - 2 * Ball.radius;
	    	break;
	    	
	    case ORANGE_AND_WHITE:
	    	this.position[0] = yellowPosition[0] + 3 * Math.sqrt(3) * Ball.radius;
	    	this.position[1] = yellowPosition[1] + Ball.radius;
	    	break;
	    	
	    case GREEN_AND_WHITE:
	    	this.position[0] = yellowPosition[0] + 4 * Math.sqrt(3) * Ball.radius;
	    	this.position[1] = yellowPosition[1] + 4 * Ball.radius;
	    	break;
	    	
	    case BROWN_AND_WHITE:
	    	this.position[0] = yellowPosition[0] + 3 * Math.sqrt(3) * Ball.radius;
	    	this.position[1] = yellowPosition[1] - 3 * Ball.radius;
	    	break;
	    }
	}
	
	public void paint(Graphics g, int size, int halfSize, int[] displacement, int[] tableDimmensions){
		 g.setColor(new Color(255, 255, 255));
		 g.fillOval((int) ( position[0] / Table.dimensions[0] * tableDimmensions[0]
				 	- halfSize + displacement[0] ),
		            (int) ( position[1] / Table.dimensions[1] * tableDimmensions[1]
					- halfSize + displacement[1] ),
		            size, size);
	}
	
	public boolean contains(int[] coords, int[] dimensions, Rectangle r) {
		double scaleFactor;
		int[] dimmensions, displacement;
        int size;
		
		if (r.width / dimensions[0] < r.height / dimensions[1]) {
		    scaleFactor = r.width / dimensions[0];
            displacement = new int[] {0, (int) ((r.height - dimensions[1] * scaleFactor) / 2)};
            dimmensions = new int[] {r.width, r.height - 2 * displacement[1]};
		} else {
            scaleFactor = r.height / dimensions[1];
            displacement = new int[] {(int) ((r.width - dimensions[0] * scaleFactor) / 2), 0};
            dimmensions = new int[] {r.width - 2 * displacement[0], r.height};
		}
		
		// TODO: Fix crude hot-fix
		size = (int) (Ball.radius * 2 * scaleFactor);
		
		return Math.sqrt(
				Math.pow(position[0] / Table.dimensions[0] * dimmensions[0] 
						+ displacement[0] - coords[0], 2) +
				Math.pow(position[1] / Table.dimensions[1] * dimmensions[1]
						+ displacement[1] - coords[1], 2)
		) < size;
	}
}
